package control
{
	import com.greensock.TweenLite;
	
	import events.MyEvent;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import loader.SinglePicLoader;
	
	import utils.MyTimer;
	public class ScrollPicsBanner_hor_v2 extends Sprite
	{
		private var pics:Vector.<Sprite>;
		private var picsContainer:Sprite;
		private var singlePicLoader:SinglePicLoader;
		private var totalWidth:Number=0;
		private var distanceX:Number=0;
		private var distanceY:Number=0;
		private var startMouseX:Number;
		private var startMouseY:Number;
		private var finalX:Number;
		private var startX:Number;
		private var loadedNumber:uint=0;
		private var pics_urls:Vector.<String>;
		private var myTimer:MyTimer;
		private var myTime:Number=0.2;
		private var _mask:Sprite;
		private var mouseContainer:Sprite;//鼠标参照容器 root?parent?
		public function ScrollPicsBanner_hor_v2(container:Sprite,mask:Sprite,_parent:Sprite)
		{
			super();
			picsContainer=container;
			_mask=mask;
			mouseContainer=_parent;
			picsContainer.mask=_mask;
		}
		
		public function addPics(arr:Vector.<String>):void
		{
			pics_urls=arr;
			loadeNext();
		}
		
		private function loadeNext():void
		{
			singlePicLoader=new SinglePicLoader(pics_urls[loadedNumber],handlePicLoaded);
		}
		
		private function handlePicLoaded(b:Bitmap):void
		{
			if(!pics)
			{
				pics=new Vector.<Sprite>(pics_urls.length);
			}
			
			var sp:Sprite;
			var bm:Bitmap=new Bitmap(b.bitmapData.clone());
			sp=new Sprite();
			sp.addChild(bm);
			sp.name="pic_"+loadedNumber;
			picsContainer.addChild(sp);
			pics[loadedNumber]=sp;
			totalWidth+=bm.width+5;
			if(loadedNumber!=0)
			{
				sp.x=pics[loadedNumber-1].x+pics[loadedNumber-1].width+5;
			}
			sp.addEventListener(MouseEvent.CLICK,handleSpClick);
			loadedNumber++;
			if(myTimer)
			{
				myTimer.GC();
				myTimer=null;
			}
			if(loadedNumber==pics_urls.length)
			{
				totalWidth-=5;
				if(this.root)
				{
					this.root.addEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
				}
			}
			else
			{
				myTimer=new MyTimer(loadeNext,myTime,1);
				myTimer.start();
			}
		}
		
		private function handleSpClick(e:MouseEvent):void
		{
			this.dispatchEvent(new MyEvent("moved", null, true));
			if(Math.abs(distanceX)<2&&Math.abs(distanceY)<2)
			{
				var name:String=e.currentTarget.name;
				var num:Number=Number(name.slice(4));
				this.dispatchEvent(new MyEvent("picChosed",num));
			}
		}
		
		private function handleMouseDown(e:MouseEvent):void
		{
			this.dispatchEvent(new MyEvent("moved", null, true));
			if(mouseContainer.mouseX<this.x+_mask.x||mouseContainer.mouseX>this.x+_mask.x+_mask.width ||mouseContainer.mouseY<this.y+_mask.y||mouseContainer.mouseY>this.y+_mask.y+_mask.height)return;
			startX=picsContainer.x;
			startMouseX=mouseContainer.mouseX;
			startMouseY=mouseContainer.mouseX;
			this.root.addEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove);
			this.root.addEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
		}
		
		private function handleMouseMove(e:MouseEvent):void
		{
			this.dispatchEvent(new MyEvent("moved", null, true));
			distanceX=startMouseX-mouseContainer.mouseX;
			distanceY=startMouseY-mouseContainer.mouseY;
			moveFunction();
		}
		
		public function moveLeft(value:Number):void
		{
			distanceX=-value;
			startX=picsContainer.x;
			moveFunction();
			back();
		}
		
		public function moveRight(value:Number):void
		{
			distanceX=value;
			startX=picsContainer.x;
			moveFunction();
			back();
		}
		
		private function moveFunction():void
		{
			var _target:Number = startX - distanceX;
			if(startX>_target)
			{
				if (_target < _mask.x + _mask.width  - totalWidth) 
				{
					_target=_mask.x + _mask.width - totalWidth-100;
				}
			}
			else if(startX<_target)
			{
				if (_target > _mask.x + _mask.width )
				{
					_target = _mask.x + 100;
				}
				
			}
			TweenLite.to(picsContainer, 0.5, { x:_target} );
			if (_target < _mask.x + _mask.width  - totalWidth) _target =   _mask.x + _mask.width  - totalWidth;
			if (_target > _mask.x)_target = _mask.x
			finalX=_target;
		}
		
		public function back():void
		{
			TweenLite.to(picsContainer,0.5,{x:finalX,onComplete:huifu});
		}
		
		private function huifu():void
		{
			distanceX=0;
			distanceY=0;
		}
		
		private function handleMouseUp(e:MouseEvent):void
		{
			back();
			
			if(this.root.hasEventListener(MouseEvent.MOUSE_MOVE))
			{
				this.root.removeEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove);
				this.root.removeEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
			}
			
		}
		
		public function gc():void
		{
			if(pics)
			{
				if(pics.length>0)
				{
					for(var i:uint=0;i<pics.length;i++)
					{
						pics[i].removeEventListener(MouseEvent.CLICK,handleSpClick);
					}
				}
				pics=null;
				
			}
			if(myTimer)
			{
				myTimer.GC();
				myTimer=null;
			}
			if(this.root)
			{
				this.root.removeEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
				if(this.root.hasEventListener(MouseEvent.MOUSE_MOVE))
				{
					this.root.removeEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove);
					this.root.removeEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
				}
			}
			
		}
	}
}